package logic

import (
	"api/internal/svc"
	"api/internal/types"
	"api/pkg"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"
)

type AddUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserLogic {
	return &AddUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddUserLogic) AddUser(r *http.Request, req *types.AddUserRequest) (resp *types.Response, err error) {
	file, header, err := r.FormFile("image")
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "上传失败",
		}, nil
	}
	defer file.Close()
	if header.Size > 1024*1024*10 {
		return &types.Response{
			Code: 500,
			Msg:  "图片过大",
		}, nil
	}
	if header.Filename != ".png.jpg" {
		return &types.Response{
			Code: 500,
			Msg:  "图片格式错误",
		}, nil
	}
	// 上传文件到七牛云
	pkg.QiNiu(file, header.Size, header.Filename)
	return &types.Response{
		Code: 200,
		Msg:  "上传成功",
	}, nil
}
